package com.bjtu.literatureclient.api;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

public enum LoginResultEnum {

    SUCCESS(0L,"登录成功"),
    UNKNOWN_ACCOUNT(1L,"帐号不存在"),
    INCORRECT_CREDENTIALS(2L,"账户存在，密码不匹配"),
    UNREGISTERED_OR_DELETED(3L,"该用户未注册或已被删除"),
    LOGGED(4L,"已登录，无需重复登录"),
    LOCKED_ACCOUNT(5L,"您所在的团队暂未授权，请联系系统管理员！"),
    INTERNAL_ERROR(6L,"内部错误,请重新尝试");






    private static final Map<Long, LoginResultEnum> ALL = new HashMap<Long, LoginResultEnum>();

    static {
        for (LoginResultEnum item : LoginResultEnum.values()) {
            ALL.put(item.getCode(), item);
        }
    }

    private Long code;
    private String message;

    LoginResultEnum(Long code, String message) {
        this.code = code;
        this.message = message;
    }

    public static LoginResultEnum instanceOf(Long value) {
        return ALL.get(value);
    }

    /**
     * 判断参数合法性
     */
    public static boolean isValidValue(Long code) {
        if (Objects.isNull(code)) {
            return false;
        }
        return Objects.nonNull(LoginResultEnum.instanceOf(code));
    }

    /**
     * 根据code获取名称
     */
    public static String getNameByValue(Long code) {
        return Objects.nonNull(instanceOf(code)) ? instanceOf(code).getMessage() : "";
    }

    public Long getCode() {
        return this.code;
    }

    public String getMessage() {
        return this.message;
    }

    /**
     * @param code
     * @return
     */
    public boolean eq(Long code) {
        if (Objects.isNull(code)) {
            return false;
        }
        return this.code.intValue() == code.intValue();
    }
}